package com.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.common.constant.Constant;
import com.common.utils.DateUtil;
import com.common.utils.Result;
import com.entity.Hint;
import com.service.HintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wty
 * @since 2022-01-07
 */
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
@RestController
@RequestMapping("/hint")
public class HintController {

    @Autowired
    private HintService hintService;

    /**
     * 修改
     */
    @RequestMapping("/updateHint")
    public Result updateHint(@RequestBody Hint hint){
        Integer id = hint.getId();
        if(null!=id){
            boolean flag = hintService.updateById(hint);
            if(flag){
                return  Result.success();
            }else{
                return  Result.error("修改失败");
            }
        }else{
            boolean insert = hintService.insert(hint);
            if(insert){
                return  Result.success();
            }else{
                return  Result.error("添加失败");
            }
        }

    }

    /**
     * 删除
     */
    @RequestMapping("/delHint/{id}")
    public Result delHint(@PathVariable("id") Integer id){
        boolean flag = hintService.deleteById(id);
        if(flag){
            return  Result.success();
        }else{
            return  Result.error("删除失败");
        }
    }

    /**
     * 根据条件查询提示
     */
    @RequestMapping("/sreachHint")
    public Result sreachHint(@RequestBody Hint hint){
        Wrapper<Hint> wr=new EntityWrapper<>();
        wr.eq("task_id",hint.getTaskId());
        Hint vo = hintService.selectOne(wr);
        return  Result.success(vo);
    }

    /**
     * 今天是否需要继续通知此任务
     */
    @RequestMapping("/changeTask")
    public Result changeTaskState(HttpServletRequest request){
        String id = request.getParameter("id");
        String state = request.getParameter("state");
        if(state.equals("1")){
            Constant.IS_INFORM_MAP.put(Integer.valueOf(id), DateUtil.getDate_yyyyMMdd());
        }else{
            Constant.IS_INFORM_MAP.remove(Integer.valueOf(id));
        }
        return Result.success() ;
    }
}

